package cc.togetherit.jcrawler.crawler;

import java.util.Map;

import org.apache.log4j.Logger;

import cc.togetherit.jcrawler.dataPool.CrawlerPool;
import cc.togetherit.jcrawler.down.AbstractDownEngine;
import cc.togetherit.jcrawler.fetch.AbstractFetchEngine;
import cc.togetherit.jcrawler.fetch.FetchRequest;
import cc.togetherit.jcrawler.parse.BaseParseEngine;

public abstract class CrawlerContext {

    Logger logger = Logger.getLogger(getClass());
    private CrawlerConfig config;

    static final int MEM = 1;

    private AbstractDownEngine downEngine;
    private AbstractFetchEngine fetchEngine;
    private BaseParseEngine parseEngine;
    private BaseParseEngine persitenceEngine;

    private CrawlerPool<String> downLoadPool;
    private CrawlerPool<FetchRequest> fetchPool;
    private CrawlerPool<FetchRequest> parsePool;
    private CrawlerPool<Map<String, Object>> persitencePool;


    public CrawlerContext(CrawlerConfig config) {
        this.config = config;
        build();
    };


    public void addFeed(String... feeds) {
        for (String fe : feeds) {
            downLoadPool.produce(fe);
        }
    }

    public CrawlerConfig getConfig() {
        return config;
    }


    public void setConfig(CrawlerConfig config) {
        this.config = config;
        downEngine.setConfig(config.getDownConfig());
        fetchEngine.setFetchConfig(config.getFetchConfig());
        parseEngine.setParseConfig(config.getParseConfig());
        persitenceEngine.setParseConfig(config.getParseConfig());
    }


    public AbstractDownEngine getDownEngine() {
        return downEngine;
    }

    public void setDownEngine(AbstractDownEngine downEngine) {
        this.downEngine = downEngine;
    }

    public AbstractFetchEngine getFetchEngine() {
        return fetchEngine;
    }

    public void setFetchEngine(AbstractFetchEngine fetchEngine) {
        this.fetchEngine = fetchEngine;
    }

    public BaseParseEngine getParseEngine() {
        return parseEngine;
    }

    public void setParseEngine(BaseParseEngine parseEngine) {
        this.parseEngine = parseEngine;
    }

    public CrawlerPool<String> getDownLoadPool() {
        return downLoadPool;
    }

    public void setDownLoadPool(CrawlerPool<String> downLoadPool) {
        this.downLoadPool = downLoadPool;
    }

    public CrawlerPool<FetchRequest> getFetchPool() {
        return fetchPool;
    }

    public void setFetchPool(CrawlerPool<FetchRequest> fetchPool) {
        this.fetchPool = fetchPool;
    }

    public CrawlerPool<FetchRequest> getParsePool() {
        return parsePool;
    }

    public void setParsePool(CrawlerPool<FetchRequest> parsePool) {
        this.parsePool = parsePool;
    }

    public CrawlerPool<Map<String, Object>> getPersitencePool() {
        return persitencePool;
    }

    public void setPersitencePool(CrawlerPool<Map<String, Object>> persitencePool) {
        this.persitencePool = persitencePool;
    }


    public BaseParseEngine getPersitenceEngine() {
        return persitenceEngine;
    }


    public void setPersitenceEngine(BaseParseEngine persitenceEngine) {
        this.persitenceEngine = persitenceEngine;
    }


    public void build() {
        if (config == null) {
            logger.error("config is NULL !!!");
            return;
        }
        buildPool();
        buildDownEngine();
        buildFetchEngine();
        buildParseEngine();
        buildPersistenceEngine();
    }

    // public void build();

    public abstract void buildPersistenceEngine();

    public abstract void buildParseEngine();

    public abstract void buildFetchEngine();

    public abstract void buildDownEngine();

    public abstract void buildPool();

    //
}
